<?php

namespace common\models\demand;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%demand_category}}".
 *
 * @property int $id
 * @property int $parent_id
 * @property string $name
 * @property string|null $abbr
 * @property string|null $thumb
 * @property string|null $description
 * @property string|null $template
 * @property string|null $link
 * @property int $listorder
 * @property int $recommend_at
 * @property int $reserved
 * @property int $navbar
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class DemandCategory extends \common\db\ActiveRecord {

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%demand_category}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['parent_id', 'listorder', 'recommend_at', 'reserved', 'navbar', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'required'],
            [['name', 'abbr', 'template'], 'string', 'max' => 50],
            [['thumb', 'description', 'link'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'parent_id' => 'Parent ID',
            'name' => 'Name',
            'abbr' => 'Abbr',
            'thumb' => 'Thumb',
            'description' => 'Description',
            'template' => 'Template',
            'link' => 'Link',
            'listorder' => 'Listorder',
            'recommend_at' => 'Recommend At',
            'reserved' => 'Reserved',
            'navbar' => 'Navbar',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    public static function findNavs($limit = 7) {
        $query = static::find()->active(static::STATUS_ACTIVE);
        $query->orderBy([
            'navbar' => SORT_DESC,
            'listorder' => SORT_DESC,
        ]);
        return $query->limit($limit)->all();
    }

    public function getIcon() {
        return Url::attach($this->thumb);
    }

    public function getMpPage() {
        return Url::to([
            '/pages/needs/main',
            'id' => $this->id
        ]);
    }

}
